﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.WeixinMP.Models;
using Microhard.WeixinMP.Msg;
using Microhard.WeixinMP.Interfaces;
using Microhard.Core;
using FluentValidation.Results;

namespace Microhard.WeixinMP.Validators
{
    public class SendToUserInfoValidator : AbstractValidator<SendToUserInfoModel>
    {
        public SendToUserInfoValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Content).NotEmpty().WithMessage(SendUserInfo.NeedContent).When(r => r.InfoID == Guid.Empty);
                RuleFor(u => u.Title).NotEmpty().WithMessage(SendUserInfo.NeedTitle).When(r => r.InfoID == Guid.Empty);
                RuleFor(u => u.Title).Length(1, 50).WithMessage(SendUserInfo.MaxTitleLength).When(r => r.InfoID == Guid.Empty);
                RuleFor(u => u.Summary).NotEmpty().WithMessage(SendUserInfo.NeedSummary).When(r => r.InfoID == Guid.Empty);
                RuleFor(u => u.Summary).Length(1, 100).WithMessage(SendUserInfo.MaxSummaryLength).When(r => r.InfoID == Guid.Empty);
                Custom(u =>
                {
                    ISendToUserInfo iUser = IoC.Resolve<ISendToUserInfo>();
                    if (iUser.IsMsgTypeDuplicate(u.ClickKey))
                    {
                        return new ValidationFailure("MsgType", "此类型已经添加");

                    }
                    return null;
                });
            });
            RuleSet("Update", () =>
            {
                RuleFor(u => u.Content).NotEmpty().WithMessage(SendUserInfo.NeedContent);
                RuleFor(u => u.Title).NotEmpty().WithMessage(SendUserInfo.NeedTitle);
                RuleFor(u => u.Title).Length(1, 50).WithMessage(SendUserInfo.MaxTitleLength);
                RuleFor(u => u.Summary).NotEmpty().WithMessage(SendUserInfo.NeedSummary);
                RuleFor(u => u.Summary).Length(1, 100).WithMessage(SendUserInfo.MaxSummaryLength);
            });
        }
    }
}
